package tests.storage;

import static org.junit.Assert.*;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import data.EventList;

import storage.FileDatabase;

public class FileDatabaseTests {
	
	private FileDatabase newDB;
	@Before
	public void setUp() throws Exception {
		newDB = new FileDatabase();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testClearDatabase() {
		assertTrue(newDB.clearDatabase());
		assertEquals(0, newDB.getListNames().length);
	}

	@Test
	public void testListNames() {
		
		//must run this function after clearing database
		EventList newList1 = new EventList("Ankit");
		EventList newList2 = new EventList("Poornima");
		EventList newList3 = new EventList("Harsha");
		
		String[] newArray = new String[3];
		newArray[0]= "Ankit";
		newArray[1]= "Poornima";
		newArray[2]= "Harsha";
		
		newDB.writeEventList(newList1);
		newDB.writeEventList(newList2);
		newDB.writeEventList(newList3);
		
		String[] namesOfLists = newDB.getListNames();
		List<String> listOfLists = Arrays.asList(namesOfLists);
		List<String> listNewArray = Arrays.asList(newArray);
		
		Collections.sort(listOfLists);
		Collections.sort(listNewArray);
		
		assertEquals(listOfLists, listNewArray);
		
	}
	
	@Test
	public void testWriteList() {
		EventList newList = new EventList();
		assertTrue(newDB.writeEventList(newList));
	}
	
	//Do not need to test this ---
	/*
	@Test
	public void testCreateDirectory() {
		
		boolean flag  = false;
		newDB.createDirectory("mydir");
		File checkDir = new File("mydir");
		if(checkDir.isDirectory())
			flag = true;
		assertTrue(flag);
	}
	
	@Test
	public void testDirectoryCheck() {
		boolean flag = true;
		newDb.createDirectory("mydir2");
		if(newDb.createDirectory("mydir2"))
				flag = false;
		assertTrue(flag);
	}

	*/

	@Test
	public void testReadList() {
		
		FileDatabase newDB = new FileDatabase();
		EventList newList = new EventList("Random");
		newDB.writeEventList(newList);
		
		EventList returnList = newDB.readList("Random");
		
		assertEquals(newList.getName(), returnList.getName());
		//FIXME also need to check if all events match
	}

}
